package com.wkbb.common.utils;

import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.RedisKeyConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.enums.SmsTypeEnum;
import com.wkbb.common.redis.RedisTemplateUtil;
import org.apache.commons.lang.StringUtils;

public class SmsCodeUtil {

    public static ErrorCode verifyCode(RedisTemplateUtil redisTemplateUtil, String mobile, Integer type, String smsCode){

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(smsCode)){
            return ErrorCode.PARAM_MISSING;
        }

        //验证手机号码正确性,验证短信验证码长度(可以不验证),验证不通过返回错误信息
        if (!VerifyUtil.isMobileNum(mobile)) {
            return ErrorCode.FORMAT_PHONE_ILLEGAL;
        }

        //从redis中获取短信验证码(发送短信时,验证码和发送时间存于redis中)
        String key = RedisKeyConstant.WKBB_TC_SMS_VERIFY + "type:"+ type +":mobile:"+mobile;
        String redisCode = "";
        if(null!=redisTemplateUtil.get(key)){
            redisCode = String.valueOf(redisTemplateUtil.get(key));
        }

        //如果短信验证码不一致,则返回登录失败,手机验证码不正确,请重新输入
        if(StringUtils.isEmpty(redisCode)){
            return ErrorCode.COMMON_VALIDATE_CODE_NOT_EXIST_OR_INVALID;
        }

        if(!smsCode.equals(redisCode)){
            return ErrorCode.COMMON_VALIDATE_CODE_NOT_EXIST;
        }

        return ErrorCode.SUCCESS;
    }

}
